using System;
using System.Collections.Generic;
using System.Text;

namespace Wrath.Xml.Objects
{
    public enum ResizeDirection
    {
        Vertical,
        Horizontal,
        Both
    }

    public class SizeLimits : IElementContainer
    {
        private Size maximum;
        private Size minimum;

        private bool dynamic;
        private bool hostcontrol;
        private ResizeDirection resize;

        public Size Maximum { get { return maximum; } }
        public Size Minimum { get { return minimum; } }
        
        public bool IsDynamic
        {
            get { return dynamic; }
            set { dynamic = value; }
        }

        public bool HostControl
        {
            get { return hostcontrol; }
            set { hostcontrol = value; }
        }

        public ResizeDirection ReSize
        {
            get { return resize; }
            set { resize = value; }
        }

        public SizeLimits(Element element)
        {
            sourceElement = element;

            Element subElement = sourceElement.FindFirstElement("maximum", false);
            if (subElement != null)
            {
                maximum = new Size(subElement);
            }

            subElement = sourceElement.FindFirstElement("minimum", false);
            if (subElement != null)
            {
                minimum = new Size(subElement);
            }

            subElement = sourceElement.FindFirstElement("dynamic", false);
            if (subElement != null)
            {
                dynamic = true;
                Element innerElement = subElement.FindFirstElement("hostcontrol", false);
                if (innerElement != null)
                {
                    hostcontrol = true;
                }
                innerElement = subElement.FindFirstElement("resize", false);
                if (innerElement != null)
                {
                    if (innerElement.Value.ToLower() == "vertical") resize = ResizeDirection.Vertical;
                    else if (innerElement.Value.ToLower() == "horizontal") resize = ResizeDirection.Horizontal;
                    else resize = ResizeDirection.Both;
                }
                else
                {
                    resize = ResizeDirection.Both;
                }
            }
        }

        #region IElementContainer Members

        private Element sourceElement;

        public Element SourceElement
        {
            get { return sourceElement; }
        }

        public void Commit()
        {
            sourceElement = new Element("sizelimits");

            if (maximum != null)
            {
                maximum.Commit();
                maximum.SourceElement.Name = "maximum";
                sourceElement.ChildElements.Add(maximum.SourceElement);
            }

            if (minimum != null)
            {
                minimum.Commit();
                minimum.SourceElement.Name = "minimum";
                sourceElement.ChildElements.Add(minimum.SourceElement);
            }

            if (dynamic)
            {
                Element subElement = new Element("dynamic");

                if (hostcontrol)
                {
                    Element host = new Element("hostcontrol");
                    subElement.ChildElements.Add(host);
                }

                Element resizeElement = new Element("resize");
                switch (resize)
                {
                    case ResizeDirection.Vertical: resizeElement.Value = "vertical"; break;
                    case ResizeDirection.Horizontal: resizeElement.Value = "horizontal"; break;
                    case ResizeDirection.Both: resizeElement.Value = "both"; break;
                }
                subElement.ChildElements.Add(resizeElement);

                sourceElement.ChildElements.Add(subElement);
            }
        }

        #endregion
    }
}
